package org.hibernate3.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate3.entity.User;

import java.util.List;

/**
 * HelloWorld 第一次测试demo
 * Created by Administrator on 2017/1/15.
 */
public class HelloWorld {
    public static void main(String[] args){
        // 实例化Configuration
        Configuration conf = new Configuration().configure();
        // 以Configuration创建SessionFactory
        SessionFactory sf = conf.buildSessionFactory();
        // 创建Session
        Session sess = sf.openSession();
        // 开始事务
        Transaction ta = sess.beginTransaction();

        // 执行查询
        List<User> users = sess.createQuery("from User").list();
        for(User user: users){
            System.out.println(user.getId() + ", " + user.getName());
        }

        // 提交事务
        ta.commit();
        // 关闭session
        sess.close();
        sf.close();
    }
}
